package com.example.springbootcaffeinedemo.common2;
import com.example.springbootcaffeinedemo.common1.Person;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;

/**
 * Created by IntelliJ IDEA2022.3.2
 * @Author: Tenghw
 * @Date: 2023/04/18  15:07
 * @Description: 测试自定义的缓存状态收集器
 */
public class MyCacheStatsCounterTest {
    public static void main(String[] args) throws InterruptedException {
        Caffeine<String, Person> caffeine = Caffeine.newBuilder()
                .maximumWeight(30)
                .recordStats(MyCacheStatsCounter::new)
                // 设置权重值规则
                .weigher((String key, Person value) -> value.getAge());

        Cache<String, Person> cache = caffeine.build();
        cache.put("one",new Person(12,"noe"));
        cache.put("two",new Person(3,"two"));
        cache.put("three",new Person(18,"three"));

        Person thw = cache.getIfPresent("thw");
        Thread.sleep(1000);
    }
}
